import { AppDataSource } from "@/config/database";
import { Services } from "@/entities/basis/services.entity";
import { ErrorHandler } from "@/middlewares/error.middleware";
import { Context } from "koa";

/** 获取服务详情 */
export async function getServiceDetail(ctx: Context) {
    const { id } = ctx.params;

    // 验证参数
    validateParams(id);

    // 获取服务详情
    const service = await getServiceById(id);

    ctx.status = 200;
    ctx.body = service;
}

/** 验证参数 */
const validateParams = (id: string) => {
    if (!id) {
        throw new ErrorHandler('服务ID不能为空', 400);
    }
}

/** 根据ID获取服务详情 */
const getServiceById = async (id: string) => {
    const serviceRepository = AppDataSource.getRepository(Services);
    
    const service = await serviceRepository.findOne({
        where: { id }
    });
    
    if (!service) {
        throw new ErrorHandler('服务不存在', 404);
    }
    
    return service;
} 